package com.autonavi.yunda.yunji.core.service;

import com.autonavi.yunda.yunji.common.exception.AmapPreconditions;
import com.autonavi.yunda.yunji.core.model.condition.InterfaceQueryCondition;
import com.autonavi.yunda.yunji.core.model.dto.InterfaceConfigDTO;
import com.autonavi.yunda.yunji.core.model.dto.ProjectDTO;
import com.autonavi.yunda.yunji.core.model.dto.UserProjectRelationDTO;
import com.autonavi.yunda.yunji.core.model.mappers.ProjectMapper;
import com.autonavi.yunda.yunji.core.model.mappers.UserProjectRelationMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author cizhong.wcs
 * @date 2022/8/10 15:53
 */
@Component
public class ProjectService {
    @Resource
    private ProjectMapper projectMapper;
    @Autowired(required = false)
    private UserProjectRelationMapper projectRelationMapper;
    @Autowired(required = false)
    private InterfaceConfigService interfaceConfigService;

    public ProjectDTO findByName(String name) {
        AmapPreconditions.checkNotNull(name);
        QueryWrapper<ProjectDTO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", name);
        return projectMapper.selectOne(queryWrapper);
    }

    public List<ProjectDTO> listAll(Long bdId) {
        LambdaQueryWrapper<ProjectDTO> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(bdId != null, ProjectDTO::getBdId, bdId);
        return projectMapper.selectList(wrapper);
    }

    public Long add(ProjectDTO projectDTO) {
        projectMapper.insert(projectDTO);
        return projectDTO.getId();
    }

    public void update(UpdateWrapper<ProjectDTO> updateWrapper) {
        projectMapper.update(null, updateWrapper);
    }

    public ProjectDTO findByProjectIdOrThrow(String projectId) {
        ProjectDTO dto = findByProjectId(projectId);
        AmapPreconditions.checkArgumentForUser(dto != null, "不存在该项目 projectId = " + projectId);
        return dto;
    }

    public ProjectDTO findByProjectId(String projectId) {
        QueryWrapper<ProjectDTO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("project_id", projectId);
        return projectMapper.selectOne(queryWrapper);
    }


    public void findByIdOrThrow(Long id) {
        ProjectDTO dto = projectMapper.selectById(id);
        AmapPreconditions.checkArgumentForUser(dto != null, "不存在该项目 id = " + id);
    }

    public List<ProjectDTO> findByProjectIds(List<String> projectIds) {
        if (projectIds.isEmpty()) {
            return new ArrayList<>();
        }
        QueryWrapper<ProjectDTO> queryWrapper = new QueryWrapper<>();

        queryWrapper.in("project_id", projectIds);
        return projectMapper.selectList(queryWrapper);
    }

    public void deleteByProjectId(String projectId) {
        ProjectDTO dto = findByProjectIdOrThrow(projectId);
        List<InterfaceConfigDTO> list = interfaceConfigService.list(InterfaceQueryCondition.builder().projectId(dto.getProjectId()).build());
        AmapPreconditions.checkArgumentForUser(list.size() == 0, "该项目存在接口,不能删除");
        projectMapper.deleteById(dto.getId());
        projectRelationMapper.delete(new QueryWrapper<UserProjectRelationDTO>().eq("project_id", dto.getProjectId()));
    }
}
